/**
 * --------------------------------------------------------------------
 * jQuery-Plugin "Image Cross-Fader"
 * http://code.google.com/p/jquery-imagecrossfader/
 * Version: 1.2.0, 2009-08-27
 * by Christophe DELIENS (G2F - GET2FUTURE)
 * 
 * http://twitter.com/cdeliens
 * chris[at]g2f[dot]be
 *
 * Copyright (c) 2009 Christophe DELIENS
 * Licensed under the MIT Licence (http://www.opensource.org/licenses/mit-license.php)
 */
(function($) {
	// TODO: defining "global" variables prevents this plugin from being usable with multiple instances
	var timerId = null;
	var imageItems = Array();
	var nextIndex = 0;
	var mouseOnContainer = false;
	var settings = null;
	var $me = null;

	$.fn.imageCrossFader = function(options) {
		settings = $.extend($.fn.imageCrossFader.defaults, options);
		
		return this.each(function() {
			$me = $(this);
			
			// finding items
			var items = $me.find('ul li');
			var itemsCount = items.size();
			imageItems = Array(itemsCount);

			// we have our items, emptying the container
			$me.empty().show();
			
			// binding mouse events
			if (settings.pauseOnHover) {
				$me.mouseenter(function() {
					mouseOnContainer = true;
					deleteTimer();
				}).mouseleave(function() {
					mouseOnContainer = false;
					createTimer();
				});
			}
			
			if (settings.index) {
				// building items index container
				var itemsIndex = $('<div></div>').attr('class', 'index').appendTo($me);
			}
			
			for (i = 0; i < itemsCount; ++i) {
				// building imageCrossFader array
				var itemHtml = $(items[i]).html();
				imageItems[i] = itemHtml;
				
				if (settings.index) {
					// adding new index for current item
					$('<p></p>').text(i +1).attr('rel', i).click(function() {
						showItem(parseInt($(this).attr('rel')));
						return false;
					}).appendTo($(itemsIndex));
				}
			}
			
			// initiating imageCrossFader with first item
			showItem(0);
		});
	};

	$.fn.imageCrossFader.defaults = {
		fadeIn: 600,
		fadeOut: 200,
		delay: 3000,
		pauseOnHover: true,
		index: true
	};
	
	function showItem(index) {
		// clearing current running timer
		deleteTimer();
		
		if (settings.index) {
			// "selecting" current item index
			$me.find('div.index p').removeClass('selected');
			$me.find('div.index p[rel=' + index + ']').addClass('selected');
		}
		
		// crossfading in new item
		$me.find('div.item').fadeOut(settings.fadeOut, function() {
			$(this).remove();
		});
		$('<div></div>').attr('class', 'item').append(imageItems[index]).appendTo($me).fadeIn(settings.fadeIn);
		
		// executing title function / displaying title from image "alt" attribute
		if (settings.title) {
			var imageLink = null;
			var imageObject = $(imageItems[index]);
			if (!imageObject.is('img')) {
				if (imageObject.is('a')) {
					imageLink = imageObject.attr('href');
				} else {
					if (imageObject.find('a')) {
						imageLink = imageObject.find('a').attr('href');
					}
				}
				imageObject = imageObject.find('img');
			}
			var imageTitle = imageObject.attr('alt');
			
			switch (typeof(settings.title)) {
				case 'function':  // custom user function
					settings.title(imageTitle, imageLink);
					break;
					
				case 'object':  // jquery object
					settings.title.text(imageTitle);
					break;
			}					
		}
		
		// next index math
		nextIndex = index +1;
		if (nextIndex >= imageItems.length) {
			nextIndex = 0;
		}
		
		if (!mouseOnContainer) {
			createTimer();
		}
	}
	
	function createTimer() {
		// setting timer for next item display
		timerId = window.setTimeout(function() {
			showItem(nextIndex);
		}, settings.delay);
	}
	
	function deleteTimer() {
		if (timerId != null) {
			window.clearTimeout(timerId);
		}
	}	
})(jQuery);